﻿
namespace TFSChangeLog.Operations.Command
{
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Windows.Input;
    using Microsoft.TeamFoundation.Client;
    using TFSChangeLog.DataAccess;
    using TFSChangeLog.Operations.Services;
    using TFSChangeLog.ViewModel;

    /// <summary>Summary description for <see cref="ShowProjectSelectorCommand"/> class.</summary>
    public class ShowProjectSelectorCommand
    {
        #region Fields

        /// <summary>Summary description for class level field of type <c>TFSChangeLog.ViewModel.MainWindowViewModel</c>.</summary>
        private MainWindowViewModel m_ViewModel;

        /// <summary>Gets the value for ShowProjectSelector.</summary>
        public ICommand ShowProjectSelector { get; private set; }

        #endregion

        #region Constructor

        /// <summary>Initializes a new instance of the <see cref="ShowProjectSelectorCommand"/> class.</summary>
        /// <param name="viewModel">TODO: Summary description for <c>viewModel</c> parameter of type <c>TFSChangeLog.ViewModel.MainWindowViewModel</c>.</param>
        public ShowProjectSelectorCommand(MainWindowViewModel viewModel)
        {
            this.m_ViewModel = viewModel;
            this.GenerateCommand();
        }

        /// <summary>Summary description for <c>ShowProjectSelectorCommand</c> method.</summary>
        private void GenerateCommand()
        {
            this.ShowProjectSelector = new RelayCommand(this.OnShowProjectSelector);
        }
        #endregion

        /// <summary>Summary description for <c>ShowProjectSelectorCommand</c> method.</summary>
        /// <param name="context">TODO: Summary description for <c>context</c> parameter of type <c>object</c>.</param>
        private void OnShowProjectSelector(object context)
        {
            using (var tpp = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false))
            {
                var result = tpp.ShowDialog();
                if (result == DialogResult.OK)
                {
                    TFSConnection.ConnectToTFS(tpp.SelectedTeamProjectCollection.Uri.ToString(), tpp.SelectedProjects[0].Name);

                    if (this.m_ViewModel.TFSServerName != tpp.SelectedTeamProjectCollection.Uri.ToString() ||
                         this.m_ViewModel.TFSProjectName != tpp.SelectedProjects[0].Name)
                    {
                        this.m_ViewModel.ResetAllUIItems();
                    }

                    var progressDialogViewModel = this.m_ViewModel.progressDialogViewModel;
                    int[] singleIterationList = { 0 };
                    progressDialogViewModel.ResetViewModel();

                    progressDialogViewModel.TokenSource = new CancellationTokenSource();

                    // Announce that work is starting
                    this.m_ViewModel.RaiseProjectSelectionWorkStartedEvent();

                    Task selectProjectTask = Task.Factory.StartNew(
                        () => SelectProjectService.DoWork(
                        singleIterationList, this.m_ViewModel.progressDialogViewModel),
                        progressDialogViewModel.TokenSource.Token);

                    // Announce that work is ending
                    selectProjectTask.ContinueWith(
                        t => this.m_ViewModel.RaiseProjectSelectionWorkEndedEvent(),
                        TaskScheduler.FromCurrentSynchronizationContext());
                }
            }
        }
    }
}
